package com.robaone.gwt.hierarcialdb.server;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONObject;


import com.robaone.dbase.hierarchial.ConfigManager;
import com.robaone.dbase.hierarchial.Config_jdo;
import com.robaone.dbase.hierarchial.Config_jdoManager;
import com.robaone.dbase.hierarchial.HDBSessionData;
import com.robaone.dbase.hierarchial.ProjectDatabase;
import com.robaone.gwt.hierarcialdb.client.HierarcialDBService;
import com.robaone.gwt.hierarcialdb.client.data.DataConstants;
import com.robaone.gwt.hierarcialdb.server.templates.HDBAdminManagerInterface;
import com.robaone.gwt.hierarcialdb.server.templates.ManagerFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class HierarcialDBServiceImpl extends RemoteServiceServlet implements
HierarcialDBService {

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	public String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
		.replaceAll(">", "&gt;");
	}

	@Override
	public String[] getMenuItems() throws Exception {
		HDBAdminManagerInterface man = ManagerFactory.newAdminManager(this);
		try{
			return man.getMenuItems();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		
	}

	@Override
	public Vector<HashMap<String,String>> getFolderContentsbyPath(String current_path) throws Exception {
		try{
			HDBAdminManagerInterface man = ManagerFactory.newAdminManager(this);
			return man.getFolderContentsbyPath(current_path);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		
	}

	@Override
	public Vector<HashMap<String,String>> getFolderContentsbyId(String id) throws Exception {
		try{
			HDBAdminManagerInterface man = ManagerFactory.newAdminManager(this);
			return man.getFolderContentsbyId(id);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		
	}

	public String getAbsolutePath(BigDecimal id) throws Exception {
		try{
			HDBAdminManagerInterface man = ManagerFactory.newAdminManager(this);
			return man.getAbsolutePath(id);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		
	}

	@Override
	public Vector<HashMap<String, String>> getFileContentsbyId(String id)
	throws Exception {
		try{
			HDBAdminManagerInterface man = ManagerFactory.newAdminManager(this);
			return man.getFileContentsbyId(id);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		
	}

	@Override
	public HashMap<String, String> setFileContents(String text, String id)
	throws Exception {
		try{
			HDBAdminManagerInterface man = ManagerFactory.newAdminManager(this);
			return man.setFileContents(text,id);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		
	}

	public String getRemoteHost() {
		return this.getThreadLocalRequest().getRemoteHost();
	}
}
